package org.social.module;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.stream.Collectors;

import com.xiaoleilu.hutool.util.MapUtil;
import org.bean.FriendInfo;
import org.bean.PlayerInfo;
import org.cfg.DiscreteDataCfg;
import org.cfg.cache.DiscreteDataCfgCache;
import org.constant.DiscreteDataID;
import org.constant.DiscreteDataKey;
import org.constant.FriendConstant;
import org.constant.ItemConstantId;
import org.error.GameErrorCode;
import org.game.log.dsp.GoodsDsp;
import org.remote.game.RemoteGameServer;
import org.social.comparable.PlayerLevelComparable;
import org.social.db.bean.Friend;
import org.social.db.bean.PlayerMap;
import org.social.db.cache.FriendCache;
import org.social.db.cache.PlayerMapCache;
import org.social.db.proxy.bean.PlayerMapProxy;
import org.utils.RemoteUtil;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.sojoys.artifact.exception.AbstractLogicModelException;
import com.sojoys.artifact.tools.ToolError;
import com.sojoys.artifact.tools.ToolMap;
import com.xiaoleilu.hutool.date.DateUtil;
import com.xiaoleilu.hutool.date.TimeInterval;
import com.xiaoleilu.hutool.log.Log;
import com.xiaoleilu.hutool.log.LogFactory;

/**
 * @author : DengYing
 * @CreateDate : 2017年12月6日 下午2:36:18
 * @Description ：好友管理器
 */
public class FriendManager {

	private FriendManager() {
	}

	private static FriendManager ME = new FriendManager();

	public static FriendManager me() {
		return ME;
	}
	
	static final Log log = LogFactory.get(FriendManager.class);
	
	public List<FriendInfo> getFriends(int pid){
		List<Friend> friends = FriendCache.me().getByPlayerId(pid);
		List<FriendInfo> infos = new ArrayList<>(friends.size());
		PlayerMap playerMap = null;
		
		DiscreteDataCfg cfg = DiscreteDataCfgCache.me().getById(DiscreteDataID.FRIEND_CONFIG);
		int applyTimeout = ToolMap.getInt("applyTimeout", cfg.getData(),7);
		Date nowDate = new Date();
		// 过期的申请
		List<Friend> timeoutApply = Lists.newArrayList();
		for (Friend friend : friends) {
			playerMap = PlayerMapCache.me().getByPid(friend.getFriendId());
			if (playerMap == null) {
				log.warn("没有找到对应的好友信息：{}", friend.getFriendId());
				continue;
			}
			// 过期的申请
			if (friend.getStatus() ==  FriendConstant.STATUS_APPLY && DateUtil.betweenDay(friend.getModifiedDate(), nowDate , true) > applyTimeout) {
				timeoutApply.add(friend);
				continue;
			}
			
			FriendInfo friendInfo = createFriendInfo(friend,0,0);
			infos.add(friendInfo);
			
			// 删除过期的申请
			FriendCache.me().deleteBatch(timeoutApply);
		}
		return infos;
	}

	

	public void operate(int pid, List<Integer> targetIds, int type,Map<Object, Object> ext){
		boolean batch = targetIds.size() > 1;
		switch (type) {
		case FriendConstant.OPERATE_APPLY_FRIEND:
			applyFriend(pid, targetIds ,batch,ext);
			break;
		case FriendConstant.OPERATE_DELETE_FRIEND:
			deleteFriend(pid, targetIds,batch,ext);
			break;
		case FriendConstant.OPERATE_SHIELD_PLAYER:
			shieldPlayer(pid, targetIds,batch,ext);
			break;
		case FriendConstant.OPERATE_LIFTED_SHIELD:
			liftedShield(pid, targetIds,batch,ext);
			break;
		case FriendConstant.OPERATE_AGREE_APPLY:
			agreeApply(pid, targetIds,batch,ext);
			break;
		case FriendConstant.OPERATE_REFUSE_APPLY:
			refuseApply(pid, targetIds,batch,ext);
			break;
		case FriendConstant.OPERATE_GIVE_GIFT:
			giveGift(pid, targetIds,batch,ext);
			break;
		case FriendConstant.OPERATE_RECEIVE_GIFT:
			receiveAway(pid, targetIds,batch,ext);
			break;
		default:
			ToolError.newLogicError(GameErrorCode.CLIENT_PARAM_IS_ERR, "参数错误");
			break;
		}
	}

	public List<PlayerInfo> getRecommendFriends(int pid) throws AbstractLogicModelException {
		TimeInterval timer = DateUtil.timer();
		// 需要过滤的玩家
		List<Integer> filterPids = Lists.newArrayList();
		filterPids.add(pid);
		List<Friend> friends = FriendCache.me().getByPlayerId(pid);
		for (Friend friend : friends) {
			if (friend.getStatus() == FriendConstant.STATUS_SHIELD || friend.getStatus() == FriendConstant.STATUS_FRIEND) {
				filterPids.add(friend.getFriendId());
			}
		}
		
		DiscreteDataCfg cfg = DiscreteDataCfgCache.me().getById(DiscreteDataID.FRIEND_CONFIG);
		int friendRecommendation = ToolMap.getInt("friendRecommendation", cfg.getData(),10);
		
		PlayerMap playerMap = PlayerMapCache.me().getByPid(pid);
		
		int nowLvl = ToolMap.getInt(PlayerInfo.LVL, playerMap.getInfo());
		int maxLvl = PlayerLevelComparable.getMaxLvl();
		int minLvl = PlayerLevelComparable.getMinLvl();
		
		Date nowDate = DateUtil.date();
		
		// 取得最大等级差，作为最大循环次数
		int maxLoop = Math.max(nowLvl - minLvl, maxLvl - nowLvl);
		
		List<Integer> recommend = Lists.newArrayList();
		
		for (int i = 0; i <= maxLoop; i++) {
			if (i==0) {
				// 平级
				recommend.addAll(friendFilter(nowLvl,filterPids, playerMap, nowDate));
			}else {
				int ceiling = nowLvl + i;
				int floor = nowLvl - i;
				
				// 上区间
				if (ceiling < maxLvl) {
					recommend.addAll(friendFilter(ceiling,filterPids, playerMap, nowDate));	
				}
				// 下区间
				if (floor >= minLvl) {
					recommend.addAll(friendFilter(floor,filterPids, playerMap, nowDate));
				}
			}
			// 达到需要数量的3倍，及停止筛选
			if (recommend.size() >= (friendRecommendation * 3)) {
				break;
			}
		}
		// 打乱顺序
		Collections.shuffle(recommend);
		
		
		maxLoop = Math.min(recommend.size(), friendRecommendation);
		
		
		List<PlayerInfo> recommendInfo = new ArrayList<>();
		
		Iterator<Integer> iterator = recommend.iterator();
		
		int i = 0;
		PlayerMap target = null;
		PlayerMapProxy proxy = null;
		
		while (iterator.hasNext() && i < maxLoop) {
			int tpid = iterator.next();
			target = PlayerMapCache.me().getByPid(tpid);
			proxy = target.getPlayerMapProxy();
			recommendInfo.add(proxy.toPlayerInfo());
			i++;
		}
		long time = timer.interval();
		if (time >= 3000) {
			log.error("getRecommendFriends timeout {}",time);
		}
		return recommendInfo;
	}

	/**
	 * 推荐好友筛选规则
	 * @param lvl
	 * @param filterPids
	 * @param playerInfo
	 * @param nowDate
	 * @return
	 */
	private Set<Integer> friendFilter(int lvl,List<Integer> filterPids, PlayerMap playerMap, Date nowDate) {
		// 先取得在线
		Set<Integer> pids = PlayerLevelComparable.getPids(lvl, true , filterPids);
		
		// 取得离线
		pids.addAll(PlayerLevelComparable.getPids(lvl, false , filterPids).stream().filter(tid -> {
			PlayerMap targetPlayerMap = PlayerMapCache.me().getByPid(tid);
			// 在30天内登陆过的玩家
			if (targetPlayerMap == null || DateUtil.betweenDay(targetPlayerMap.getPlayerMapProxy().getLastLoginDate(), nowDate, true) > 30) {
				return false;
			}
			return true;
		}).collect(Collectors.toSet()));
		
		return pids;
	}
	
	/**
	 * 申请好友
	 * 
	 * @param pid
	 * @param targetIds
	 */
	private void applyFriend(int pid, List<Integer> targetIds,boolean batch,Map<Object, Object> ext) {
		Date nowDate = new Date();
		// 服务器-玩家-状态-好友信息集
		Map<String, Map<Integer, List<FriendInfo>>> result = Maps.newHashMap();
		for (Integer tid : targetIds) {
			Friend friend = FriendCache.me().getByPlayerIdFriendId(tid, pid);
			if (friend != null) {
				switch (friend.getStatus()) {
				case FriendConstant.STATUS_FRIEND:
					ToolError.isAndTrue(GameErrorCode.YOU_ARE_ALREADY_FRIENDS,"你们已经是好友", !batch);
					break;
				case FriendConstant.STATUS_SHIELD:
					// 如果对方屏蔽了，就忽略此申请
					ToolError.isAndTrue(GameErrorCode.YOU_HAVE_BEEN_SHIELDED,"你被对方屏蔽", !batch);
					continue;
				case FriendConstant.STATUS_APPLY:
					// 修改申请时间
					friend.update();
					break;
				default:
					break;
				}
			}else {
				// 添加申请请求
				friend = new Friend(0, tid, pid, FriendConstant.STATUS_APPLY,false,DateUtil.date());
				friend.insert();
				
				addNotifyFriend(result, nowDate, tid, friend,FriendConstant.STATUS_ADD);
			}
		}
		
		notifyServers(result,ext);
	}
	
	/**
	 * 删除好友
	 * @param pid
	 * @param targetIds
	 */
	private void deleteFriend(int pid, List<Integer> targetIds,boolean batch,Map<Object, Object> ext) {
		// 服务器-玩家-状态-好友信息集
		Map<String, Map<Integer, List<FriendInfo>>> result = Maps.newHashMap();
		
		deleteFriend(pid, targetIds, result,batch);
		
		notifyServers(result,ext);
	}


	
	private void deleteFriend(int pid, List<Integer> targetIds,Map<String, Map<Integer, List<FriendInfo>>> result,boolean batch) {
		Date nowDate = new Date();
		
		for (Integer tid : targetIds) {
			Friend friend = FriendCache.me().getByPlayerIdFriendId(pid, tid);
			if (friend != null) {
				friend.delete();
				addNotifyFriend(result, nowDate, pid, friend,FriendConstant.STATUS_DELETE,FriendConstant.FRIEND_DEL_EVENT);
				
				
				friend = FriendCache.me().getByPlayerIdFriendId(tid,pid);
				if (friend != null) {
					friend.delete();
					addNotifyFriend(result, nowDate, tid, friend,FriendConstant.STATUS_DELETE,FriendConstant.FRIEND_DEL_EVENT);
				}
			}else {
				ToolError.isAndTrue(GameErrorCode.NOT_FOND_FRIEND, "好友信息不存在",!batch);
			}
		}
	}


	
	
	/**
	 * 屏蔽玩家
	 * @param pid
	 * @param targetIds
	 */
	private void shieldPlayer(int pid, List<Integer> targetIds,boolean batch,Map<Object, Object> ext) {
		
		// 好友离散配置
		DiscreteDataCfg cfg = DiscreteDataCfgCache.me().getById(DiscreteDataID.FRIEND_CONFIG);
		int maxBlacklistNum = ToolMap.getInt("maxBlacklistNum", cfg.getData(),100);
		
		Date nowDate = new Date();
		
		Map<String, Map<Integer, List<FriendInfo>>> result = Maps.newHashMap();
		// 删除好友
		deleteFriend(pid, targetIds,result, true);
		// 删除申请
		refuseApply(pid, targetIds, result,true);
		
		for (Integer tid : targetIds) {
			Friend friend = FriendCache.me().getByPlayerIdFriendId(pid, tid);
			if (friend != null) {
				if (friend.getStatus() == FriendConstant.STATUS_SHIELD) {
					continue;
				}
			}else {
				int nowBlacklistNum = FriendCache.me().getByPlayerIdStatus(pid, FriendConstant.STATUS_SHIELD).size();
				if (nowBlacklistNum > maxBlacklistNum) {
					if (!batch) {
						ToolError.newLogicError(GameErrorCode.HAS_REACHED_BLACK_LIMIT);
					}else {
						break;
					}
				}
				// 添加屏蔽
				friend = new Friend(0, pid, tid, FriendConstant.STATUS_SHIELD,false,DateUtil.date());
				friend.insert();
				
				addNotifyFriend(result, nowDate, pid, friend,FriendConstant.STATUS_ADD);
			}
		}
		
		notifyServers(result,ext);
	}
	
	/**
	 * 取消屏蔽玩家
	 * @param pid
	 * @param targetIds
	 */
	private void liftedShield(int pid, List<Integer> targetIds,boolean batch,Map<Object, Object> ext){
		Map<String, Map<Integer, List<FriendInfo>>> result = Maps.newHashMap();
		Date nowDate = new Date();
		for (Integer tid : targetIds) {
			Friend friend = FriendCache.me().getByPlayerIdFriendIdStatus(pid, tid,FriendConstant.STATUS_SHIELD);
			if (friend != null) {
				friend.delete();
				addNotifyFriend(result, nowDate, pid, friend,FriendConstant.STATUS_DELETE);
			}else {
				ToolError.isAndTrue(GameErrorCode.NOT_FOND_SHIELD_FRIEND, "你没有屏蔽该玩家",!batch);
			}
		}
		
		
		notifyServers(result,ext);
	}
	
	
	/**
	 * 同意申请
	 * @param pid
	 * @param targetIds
	 */
	private void agreeApply(int pid, List<Integer> targetIds,boolean batch,Map<Object, Object> ext){
		
		Date nowDate = new Date();
		Map<String, Map<Integer, List<FriendInfo>>> result = Maps.newHashMap();
		
		DiscreteDataCfg cfg = DiscreteDataCfgCache.me().getById(DiscreteDataID.FRIEND_CONFIG);
		int maxFriendsNum = ToolMap.getInt("maxFriendsNum", cfg.getData(),100);
		boolean isTarMax = false;
		
		for (Integer tid : targetIds) {
			int nowFriendCount = FriendCache.me().getByPlayerIdStatus(pid, FriendConstant.STATUS_FRIEND).size();
			if (nowFriendCount >= maxFriendsNum) {
				if (batch) {
					break;
				}else {
					ToolError.isAndTrue(GameErrorCode.HAS_REACHED_FRIENDS_LIMIT, "已达好友上限",true);
				}
			}
			
			Friend friend = FriendCache.me().getByPlayerIdFriendIdStatus(pid, tid,FriendConstant.STATUS_APPLY);
			if (friend != null) {
				// 判断对方好友上限
				int targetFriendCount = FriendCache.me().getByPlayerIdStatus(friend.getFriendId(), FriendConstant.STATUS_FRIEND).size();
				if (targetFriendCount >= maxFriendsNum) {
					isTarMax = true;
					continue;
				}
				// 双方互加好友
				friend.setStatus(FriendConstant.STATUS_FRIEND);
				friend.setLastGiveDate(DateUtil.offsetDay(nowDate, -1));
				friend.update();
				
				addNotifyFriend(result, nowDate, pid, friend,FriendConstant.STATUS_UPDATE,FriendConstant.FRIEND_ADD_EVENT);
				
				
				// 删除申请
				List<Integer> pids = new ArrayList<>(1);
				pids.add(pid);
				refuseApply(tid, pids, result,true);
				
				friend = new Friend(0, friend.getFriendId(), pid, FriendConstant.STATUS_FRIEND,false,DateUtil.offsetDay(nowDate, -1));
				friend.insert();
				
				addNotifyFriend(result, nowDate, tid, friend,FriendConstant.STATUS_ADD,FriendConstant.FRIEND_ADD_EVENT);
			}else {
				ToolError.isAndTrue(GameErrorCode.NOT_FOND_FRIEND_APPLY, "没有找到好友申请信息",!batch);
			}
		}
		notifyServers(result,ext);
		if(isTarMax){
			ToolError.isAndTrue(GameErrorCode.TARGET_HAS_REACHED_FRIENDS_LIMIT, "对方好友已达上限",true);
		}
	}



	private void notifyServers(Map<String, Map<Integer, List<FriendInfo>>> result,Map<Object, Object> ext) {
		for (Entry<String, Map<Integer, List<FriendInfo>>> e : result.entrySet()) {
			RemoteGameServer gameServer = RemoteUtil.gameAsyn(e.getKey());
			gameServer.notifyFriendInfos(e.getValue(),ext);
		}
	}
	
	/**
	 * 拒绝申请
	 * @param pid
	 * @param targetIds
	 */
	private void refuseApply(int pid, List<Integer> targetIds,boolean batch,Map<Object, Object> ext){
		
		Map<String, Map<Integer, List<FriendInfo>>> result = Maps.newHashMap();
		refuseApply(pid, targetIds, result ,batch);
		
		notifyServers(result,ext);
	}



	private void refuseApply(int pid, List<Integer> targetIds,Map<String, Map<Integer, List<FriendInfo>>> result,boolean batch) {
		Date nowDate = new Date();
		for (Integer tid : targetIds) {
			Friend friend = FriendCache.me().getByPlayerIdFriendIdStatus(pid, tid,FriendConstant.STATUS_APPLY);
			if (friend != null) {
				friend.delete();
				addNotifyFriend(result, nowDate, pid, friend,FriendConstant.STATUS_DELETE);
			}else {
//				ToolError.isAndTrue(GameErrorCode.NOT_FOND_FRIEND_APPLY, "没有找到好友申请信息",!batch);
			}
		}
	}
	
	/**
	 * 赠送礼品
	 * @param pid
	 * @param targetIds
	 */
	private void giveGift(int pid, List<Integer> targetIds,boolean batch,Map<Object, Object> ext){
		Date nowDate = new Date();
		// 服务器-玩家-状态-好友信息集
		Map<String, Map<Integer, List<FriendInfo>>> result = Maps.newHashMap();
		for (Integer tid : targetIds) {
			Friend friend = FriendCache.me().getByPlayerIdFriendId(pid, tid);
			if (friend != null) {
				// 不是好友
				if (friend.getStatus() != FriendConstant.STATUS_FRIEND) {
					if (!batch) {
						ToolError.newLogicError(GameErrorCode.CLIENT_PARAM_IS_ERR);
					}else {
						continue;
					}
				}
				
				// 已经赠送过了
				if (DateUtil.betweenDay(friend.getLastGiveDate(), nowDate , true) <= 0) {
					if (!batch) {
						ToolError.newLogicError(GameErrorCode.TODAY_HAS_BEEN_PRESENTED);
					}else {
						continue;
					}
				}
				
				Friend target = FriendCache.me().getByPlayerIdFriendId(tid,pid);
				if (target != null) {
					target.setGift(true);
					target.update();
					
					addNotifyFriend(result, nowDate, tid, target,FriendConstant.STATUS_UPDATE);
					
					
					friend.setLastGiveDate(nowDate);
					friend.update();
					addNotifyFriend(result, nowDate, pid, friend,FriendConstant.STATUS_UPDATE);
				}
			}
		}
		notifyServers(result,ext);
	}
	
	/**
	 * 收取礼品
	 * @param pid
	 * @param targetIds
	 */
	private void receiveAway(int pid, List<Integer> targetIds,boolean batch,Map<Object, Object> ext){
		Date nowDate = new Date();
		Date lastReceiveTime = ToolMap.getDate("lastReceiveTime", ext , nowDate);
		int receiveCount = ToolMap.getInt("receiveCount", ext ,0);

		// 跨天重置
		if(DateUtil.betweenDay(lastReceiveTime, nowDate , true) > 0){
			receiveCount = 0;
		}
		
		DiscreteDataCfg cfg = DiscreteDataCfgCache.me().getById(DiscreteDataID.FRIEND_CONFIG);
		int receiveFriendshipTime = ToolMap.getInt("receiveFriendshipTime", cfg.getData(),60);
		
		// 服务器-玩家-状态-好友信息集
		Map<String, Map<Integer, List<FriendInfo>>> result = Maps.newHashMap();
		
		int count = 0;
		for (Integer tid : targetIds) {
			Friend friend = FriendCache.me().getByPlayerIdFriendId(pid, tid);
			if (friend != null) {
				
				if (friend.getStatus() != FriendConstant.STATUS_FRIEND) {
					if (!batch) {
						ToolError.newLogicError(GameErrorCode.CLIENT_PARAM_IS_ERR);
					}else {
						continue;
					}
				}
				
				
				if (!friend.isGift()) {
					if (!batch) {
						ToolError.newLogicError(GameErrorCode.NO_REWARD_TO_RECEIVE);
					}else {
						continue;
					}
				}
				
				if (receiveCount+count >= receiveFriendshipTime) {
					if (!batch) {
						ToolError.newLogicError(GameErrorCode.MAX_RECEIVE_COUNT);
					}else {
						break;
					}
				}
				
				friend.setGift(false);
				friend.update();
				count++;
				addNotifyFriend(result, nowDate, pid, friend,FriendConstant.STATUS_UPDATE);
			}
		}
		ext.put("lastReceiveTime", nowDate);
		ext.put("receiveCount", receiveCount+count);
		ext.put("awayCount", count);
		ext.put("receiveAway", true);
		notifyServers(result,ext);
	}
	
	/**
	 * 创建好友信息
	 * @param friend
	 * @return
	 */
	private FriendInfo createFriendInfo(Friend friend,int ct,int friendAction) {
		PlayerMap playerMap = PlayerMapCache.me().getByPid(friend.getFriendId());
		if (playerMap == null) {
			return null;
		}
		PlayerMapProxy proxy = playerMap.getPlayerMapProxy();
		FriendInfo friendInfo = new FriendInfo();
		// 封装玩家信息
		friendInfo.setPid(playerMap.getPid());
		friendInfo.setName(proxy.getName());
		friendInfo.setLeaderCid(proxy.getHelpHeroCid());
		friendInfo.setFightPower(proxy.getFightPower());
		friendInfo.setLvl(proxy.getPlv());
		friendInfo.setLastLoginDate(proxy.getLastOfflineDate());
		// 封装好友特有信息
		friendInfo.setLastHandselTime(friend.getLastGiveDate());
		friendInfo.setReceive(friend.isGift());
		friendInfo.setStatus(friend.getStatus());
		// 封装在线信息
		friendInfo.setOnline(playerMap.getPlayerMapProxy().isOnline());
		
		switch (friend.getStatus()) {
		case FriendConstant.STATUS_FRIEND:
			friendInfo.setCreateDate(friend.getCreateDate());
			break;
		case FriendConstant.STATUS_SHIELD:
			friendInfo.setCreateDate(friend.getCreateDate());		
			break;
		case FriendConstant.STATUS_APPLY:
			friendInfo.setCreateDate(friend.getModifiedDate());
			break;
		}
		friendInfo.setCt(ct);
		
		friendInfo.setFriendAction(friendAction);
		
		return friendInfo;
	}
	
	
	/**
	 * 添加好友推送信息
	 * @param result
	 * @param nowDate
	 * @param targetPid
	 * @param friend
	 * @param status
	 */
	private void addNotifyFriend(Map<String, Map<Integer, List<FriendInfo>>> result, Date nowDate, Integer targetPid,Friend friend,int ct) {
		addNotifyFriend(result, nowDate, targetPid, friend, ct,0);
	}
	
	private void addNotifyFriend(Map<String, Map<Integer, List<FriendInfo>>> result, Date nowDate, Integer targetPid,Friend friend,int ct,int friendAction) {
		PlayerMap playerMap = PlayerMapCache.me().getByPid(targetPid);
		if (playerMap == null) {
			return;
		}
		// 封装服务器
		Map notifyServer = result.computeIfAbsent(playerMap.getServerId(), k -> Maps.newHashMap());
		// 封装人
		List changeList = (List) notifyServer.computeIfAbsent(targetPid, k -> Lists.newArrayList());

		FriendInfo friendInfo = createFriendInfo(friend,ct,friendAction);
		if (friendInfo != null) {
			changeList.add(friendInfo);			
		}
	}
	
	/**
	 * 通知好友信息变更
	 * @param playerMap
	 */
	public void notifyFriends(PlayerMap playerMap){
		Map<String, Map<Integer, List<FriendInfo>>> result = Maps.newHashMap();
		Date nowDate = new Date();
		List<Friend> friends = FriendCache.me().getByPlayerIdStatus(playerMap.getPid(), FriendConstant.STATUS_FRIEND);
		for (Friend friend : friends) {
			friend = FriendCache.me().getByPlayerIdFriendId(friend.getFriendId(), friend.getPlayerId());
			addNotifyFriend(result, nowDate, friend.getPlayerId(), friend,FriendConstant.STATUS_UPDATE);	
		}
		notifyServers(result, new HashMap<>());
	}

	/**
	 * 助战奖励友情点
	 * @param pid
	 * @param friendPid
	 */
	public void rewardFriendShipPointByHelpFight(int pid, int friendPid){
		Friend friend = FriendCache.me().getByPlayerIdFriendId(pid,friendPid);
		PlayerMap playerMap = PlayerMapCache.me().getByPid(friendPid);
		if(playerMap == null){
			return;
		}
		int friendShipPoint = getFriendShipPoint(friend != null);
		RemoteUtil.gameSync(playerMap.getServerId()).changeItem(playerMap.getPid(), MapUtil.of(ItemConstantId.FRIEND_SHIP_POINT,friendShipPoint), GoodsDsp.HELP_FIGHT,true);
	}

	/**
	 * 获取应该奖励的友情点
	 * @param isFriend 是否好友
	 */
	private int getFriendShipPoint(boolean isFriend){
		String key = null;
		if (isFriend){
			key = DiscreteDataKey.FRIEND_ADD_FRIEND_POINT;
		}else{
			key = DiscreteDataKey.STRANGER_ADD_FRIEND_POINT;
		}
		return DiscreteDataCfgCache.me().getIntData(DiscreteDataID.HELP_FIGHTER, key);
	}

}
